package com.wanmait.schedule.controller;

import com.wanmait.schedule.entity.Course;
import com.wanmait.schedule.service.CourseService;
import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 全部课程表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/manage/course")
@RequiresPermissions("manage:course")
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 根据ID获得
     * @param id
     * @return
     */
    @GetMapping("findBy/{id}")
    public AjaxResult findById(@PathVariable("id") Integer id){
        Course byId = courseService.getById(id);
        return AjaxResult.success("成功").put("course",byId);
    }

    //全部课程列表
    @GetMapping("list")
    public AjaxResult list(){
        return AjaxResult.success(courseService.list());
    }

    //课程的增加  修改
    @PostMapping("save")
    public AjaxResult save(@RequestBody Course course){
        if(course.getId()==null){
//            List<Course> courses=courseService.list();
//            for (Course i:courses){
//                if(i.getCourseName().equals(course.getCourseName())){
//                    return AjaxResult.error(10001,"该课程已存在");
//                }
//            }
            courseService.save(course);
        }
        else {
            courseService.updateById(course);
        }
        return AjaxResult.success("保存成功",course);
    }

    //课程的删除
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        courseService.removeById(id);
        return  AjaxResult.success("删除成功");
    }

    //课程总数
    @GetMapping("total")
    public AjaxResult total(){
        return AjaxResult.success("成功",courseService.count());
    }
}
